<template>
  <el-dialog :title="title" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('system.menu.icon')" prop="iconPath">
        <el-input v-model="formValidate.iconPath" clearable :placeholder="$t('common.input')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('system.menu.path')" prop="url">
        <el-input v-model="formValidate.url" clearable :placeholder="$t('common.input')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('system.menu.button')" prop="isButton">
        <el-select v-model="formValidate.isButton" clearable :placeholder="$t('common.select')">
          <el-option
            v-for="item in $t('system.menu.is')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('system.menu.china')" prop="label">
        <el-input v-model="formValidate.label" clearable :placeholder="$t('common.input')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('system.menu.indonesia')" prop="label2">
        <el-input v-model="formValidate.label2" clearable :placeholder="$t('common.input')" :maxlength="50" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addTreeItem, editTreeItem } from '@/api/system'
export default {
  name: 'MenuSetting',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      file: null,
      submitLoading: false,
      formValidate: {
        iconPath: null,
        id: null,
        isButton: null,
        parentId: null,
        label: null,
        label2: null,
        url: null
      },
      rules: {
        iconPath: [{ required: true, message: this.$t('system.menu.icon') + this.$t('common.null'), trigger: 'blur' }],
        url: [{ required: true, message: this.$t('system.menu.path') + this.$t('common.null'), trigger: 'blur' }],
        isButton: [{ type: 'number', required: true, message: this.$t('system.menu.button') + this.$t('common.null'), trigger: 'change' }],
        label: [{ required: true, message: this.$t('system.menu.china') + this.$t('common.null'), trigger: 'change' }],
        label2: [{ required: true, message: this.$t('system.menu.indonesia') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const formData = this.formValidate
          const postObj = {
            url: formData['url'],
            iconPath: formData['iconPath'],
            isButton: formData['isButton'],
            menuDescriptionVOS: [
              {
                description: formData['label'],
                locale: 'zh_CN'
              },
              {
                description: formData['label2'],
                locale: 'id_ID'
              },
              {
                description: formData['label2'],
                locale: 'in_ID'
              }
            ]
          }
          if (formData.parentId) postObj['parentId'] = formData.parentId
          if (formData.id) postObj['id'] = formData.id
          if (postObj.id) {
            editTreeItem(postObj)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addTreeItem(postObj)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
